<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
      .box {
        position: relative;
        width: 400px;
        border: 1px solid #ccc;
        margin: 100px auto;
        padding: 2px;
      }
      .box input {
        width: 370px;
        height: 30px;
        border: 0;
        outline: none;
      }
      .box img {
        position: absolute;
        top: 4px;
        right: 6px;
        width: 24px;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <label for="">
        <img src="images/close.png" alt="" id="eye">
      </label>
      <input type="password" name="" id="pwd">
    </div>
    <script>
      // 1. 获取元素
      var eye = document.getElementById('eye');
      var pwd = document.getElementById('pwd');
      // 2. 注册事件处理程序
      var flag = 0;
      eye.onclick = function () {
        // 每次单击，修改flag的值
        if (flag == 0) {
          pwd.type = 'text';
          eye.src = 'images/open.png';
          flag = 1;
        } else {
          pwd.type = 'password';
          eye.src = 'images/close.png';
          flag = 0;
        }
      };
    </script>
  </body>
</html>